package com.situ.ymall.admin.mapper;

import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import com.situ.ymall.admin.entity.Goods;
import org.springframework.stereotype.Repository;


@Repository
public interface GoodsMapper {
    @Delete({
        "delete from goods",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

   @Insert({
       "insert into goods (goods_sort_id, ",
        "name, dscp, price, ",
        "version, color, ",
        "suit, count, status)",
        "values (#{goodsSortId},#{name}, #{dscp}, #{price}, ",
        "#{version}, #{color}, ",
        "#{suit}, #{count}, #{status})"
    })
    @Options(useGeneratedKeys = true ,keyColumn = "id",keyProperty = "id" )
    int insert(Goods goods);

  @Select({
        "select",
        "id, goods_sort_id, name, dscp, price, version, color, suit, count, status",
        "from goods",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
        @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    Goods selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, goods_sort_id, name, dscp, price, version, color, suit, count, status",
        "from goods"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
        @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    List<Goods> selectAll();

     @Update({"<script>",
        "update goods",
           "<set>",
            "<if test='goodsSortId != null'>goods_sort_id = #{goodsSortId}, </if>",
              "<if test='name != null'>name = #{name},</if>",
              "<if test='dscp != null'>dscp = #{dscp},</if>",
                "<if test='dscp == null'>dscp = null,</if>",
              "<if test='price != null'>price = #{price},</if>",
              "<if test='version != null'>version = #{version},</if>",
                "<if test='version == null'>version =  null,</if>",
              "<if test='color != null'>color = #{color},</if>",
                "<if test='color == null'>color = null,</if>",
              "<if test='suit != null'>suit = #{suit},</if>",
              "<if test='suit == null'>suit = null,</if>",
              "<if test='count != null'>count = #{count},</if>",
              "<if test='status != null'>status = #{status},</if>",
            "</set>",
            "where id = #{id,jdbcType=INTEGER}",
            "</script>"
    })
    int updateByPrimaryKey(Goods record);

     @Select({"<script>",
            "select",
            "id, goods_sort_id, name, dscp, price, version, color, suit, count, status",
            "from goods",
            "<where>",
            "<if test='goodsSortId != null'>goods_sort_id = #{goodsSortId},</if>",
            "<if test='name != null'>name like '%${name}%',</if>",
            "<if test='price != null'>price=#{price},</if>",
            "<if test='count != null'>count=#{count},</if>",
            "<if test='status != null'>status=#{status}</if>",
            "</where>",
            "</script>"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
            @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
            @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
            @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
            @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
            @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column = "goods_sort_id" ,property = "goodsSort",
                 one = @One(select = "com.situ.ymall.admin.mapper.GoodsSortMapper.selectByPrimaryKey")),
            @Result(column = "id",property = "pictures",
                many = @Many(select = "com.situ.ymall.admin.mapper.GoodsPictureMapper.selectByGoodsId"))
    })
    List<Goods> select(Goods goods);


}
